package com.lianniu.mgr.product.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductSpecValueVo;
import com.lianniu.mgr.product.model.ProductSpecVo;
import com.lianniu.mgr.product.service.IProductSpecService;
import com.lianniu.mgr.product.service.IProductSpecValueService;
@Controller
@RequestMapping("/rest/ProductValue")
public class ProductSpecValueRestController {
	
	private final static Logger logger = Logger.getLogger(ProductSpecValueRestController.class);
	  
	
	  @Autowired
	  private IProductSpecValueService psvService;
	  @Autowired
	  private IProductSpecService psService;
	  
	  @RequestMapping(value="/ProductSpecValueList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult ProductSpecValueList(HttpServletRequest request, ProductSpecVo psVo, PaginationInfo paginationInfo)
	  {
	       logger.info("ProductSpecValueRestController  ProductSpecValueList begin.");
	       try {
	           
	           logger.info("ProductSpecValueList begin.");          
	           //BaseResult baseResult = psService.selectByProductSpecForPage(psVo, paginationInfo);
	          BaseResult baseResult = psvService.selectById(psVo.getSpecId());
	           logger.info("ProductSpecValueList end.");
	           logger.info("ProductSpecValueRestController ProductSpecValueList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ProductSpecValueRestController ProductSpecValueList error:" + e.getMessage());
	       logger.error("ProductSpecValueRestController ProductSpecValueList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	  
	  @RequestMapping(value = {"/ProductValueAdd"}, method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult dictionaryAdd(HttpServletRequest request,ProductSpecValueVo psvVo) {
	      logger.info("ProductValueRestController ProductSpecValueAdd begin.");      
	      logger.info("ProductValueRestController ProductSpecValueAdd:"+ JSON.toJSONString(psvVo));
	      try {
	          logger.info("ProductSpecValueAdd begin.");
	          
	          // 保存到数据库中
	          	String[] specValueId = request.getParameterValues("specValueId");
				String specId = request.getParameter("specId");
				String[] specValueName = request.getParameterMap().get("specValueName");
				String[] sortNum =request.getParameterMap().get("sortNum");
				String[] isValidate = request.getParameterMap().get("isValidate");
				String[] remark = request.getParameterMap().get("remark");
				if(specValueId != null){
				for(int i=0;i<specValueName.length-specValueId.length;i++){
						psvVo.setSpecId(specId);
						psvVo.setSpecValueName(specValueName[specValueId.length+i]);
						psvVo.setIsValidate(isValidate[specValueId.length+i]);
						psvVo.setDelFlag("1");
						psvVo.setRemark(remark[specValueId.length+i]);
						psvVo.setSortNum(sortNum[specValueId.length+i]);
						psvService.insertProductSpecValue(psvVo);
					}	
				}else{
					for(int i=0;i<specValueName.length;i++){
						psvVo.setSpecId(specId);
						psvVo.setSpecValueName(specValueName[i]);
						psvVo.setIsValidate(isValidate[i]);
						psvVo.setDelFlag("1");
						psvVo.setRemark(remark[i]);
						psvVo.setSortNum(sortNum[i]);
						psvService.insertProductSpecValue(psvVo);
					}

				}
	          BaseResult baseResult = psvService.selectById(psvVo.getSpecId());
	          logger.info("ProductSpecValueAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductSpecValueAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductSpecValueAdd error:"+e.getMessage());
	      logger.info("ProductSpecValueRestController ProductSpecValueAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  } 
	  
	  
	  @RequestMapping(value="/selectValueById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectSpecValueById(HttpServletRequest request,ProductSpecValueVo psvVo) {
	        logger.info("ProductSpecValueRestController selectById begin.");
	        try {
	                    
	          BaseResult baseResult = psvService.selectById(psvVo.getSpecId());
	          logger.info("selectDictionaryById end.");
	          logger.info("ProductSpecValueRestController selectById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductSpecValueRestController selectById error:" + e.getMessage());
	          logger.error("ProductSpecValueRestController selectById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	
	  @RequestMapping(value = {"/ProductSpecValueDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductValueDel(HttpServletRequest request) {
	      logger.info("ProductSpecValueRestController ProductSpecValueDel begin.");
	     try {          
	          logger.info("ProductSpecValueDel begin.");
	          BaseResult baseResult = psvService.deleteById(request.getParameter("specValueId"));
	          logger.info("valueId"+request.getParameter("valueId"));
	          logger.info("ProductSpecValueDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductSpecValueDel end.");
	          
	          logger.info("ProductSpecValueRestController ProductSpecValueDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductSpecValueRestController ProductSpecValueDel error:" + e.getMessage());
	      logger.info("ProductSpecValueRestController ProductSpecValueDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	  @RequestMapping(value = {"/ProductSpecValueEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductSpecValueEdit(HttpServletRequest request,ProductSpecValueVo psvVo) {
	      logger.info("ProductSpecValueRestController ProductSpecValueEdit begin.");      
	      logger.info("ProductSpecValueRestController ProductSpecValueEdit:"+ JSON.toJSONString(psvVo));
	      try {
	          logger.info("ProductSpecValueEdit begin.");
	          BaseResult baseResult = psvService.updateBySpecValueId(psvVo);
	          logger.info("ProductSpecValueEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("updateById" +psvVo.getSpecValueId());
	          logger.info("ProductSpecValueEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductSpecValueEdit error:"+e.getMessage());
	      logger.info("ProductSpecValueRestController ProductSpecValueEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }  

}
